package com.ks.controller;

import com.ks.pojo.Dept;
import com.ks.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*
|---------------------------------------------------
|
|   提供 Restful 服务
|
|===================================================
 */

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){

        final Dept dept = deptService.queryById(id);

        if (dept==null){
            throw new RuntimeException("id=>"+id+"的用户不存在~");
        }
        return dept;
    }

    /**
     * 根据id查询备选方案(熔断)
     * @param id
     * @return
     */
    public Dept hystrixGet(@PathVariable("id") Long id){
        return new Dept().setDeptno(id)
                .setDname("id=>"+id+"的用户不存在-----------------@Hystrix~")
                .setDb_source("在MySQL中没有这个数据库");
    }

}
